#ifndef NAMENO_H
#define NAMENO_H

// FIXME: remove unnedeed stuff:
#include "ImageShack.h"
#include "PostData.h"
#include "PluginsInterface.h"
#include "ui_ImageShackTab.h"
#include <QtGui>
#include <QRadioButton>
#include <QString>
#include <QHttp>
#include <QFile>
#include <QXmlDefaultHandler>
#include <QSettings>




struct HostingParams
{
	QString Host;
	QString URI;
	QString URL;
	QString User_Agent;
};


class ImageShackClass : public QObject, uploadPluginClass
{
	Q_OBJECT
	Q_INTERFACES(uploadPluginClass)

public:
	ImageShackClass();	// oh, yeah, baby. I don't need constructor at all
			//but it is the only place to initialise some structures
	QLayout* getSettingsTab(QWidget *Tab); // return own settings page.
	void startUpload(QList<ImageToUpload> images, signalSlotStratum *Stratum); // don't really start, but
			// just prepares some stuff and call startNextFile()

private slots:
	void httpRequestFinished(int requestId, bool error); // file uploaded, start parse answer
							// first, reclose-open buffer and some stuff
	void readResponseHeader(const QHttpResponseHeader &responseHeader); // check wether server allow us to upload :)
	void startNextFile(); // acts iterations for every image
	void Canceled(); // slot for 'cancel' button
	void globalDestruction(QString errorMsg); // on total Error

// config tab events
	void resizeTogled(bool);
	void resizePolicySelected(int);
	void rembarTogled(bool);

private:
	Ui::MySettingsTab MySettingsTab; // own tab in interface with personal settings
	QSettings *Config;
	QStringList resizes; // available values for 'optimage'
	HostingParams Hosting; // some text about hosting
	signalSlotStratum *Stratum; // pointer to progress bar. also provides signals to main programm

	int currentFileIndex; // hope it will work
	ImageToUpload currentImageUploaded;
	qint64 totalSize; // size of images
	qint64 uploadedSize; // for progress bar...
	qint64 currentImageSize; // is it best way?


	QHttp *http_h; // http handler.
	bool httpRequestAborted; // http uploadig process was aborted for some reasons, usually by user request
	bool httpFileAborted;
	int httpPostId; //http is async process. this is an ID for slots.
	QBuffer *serverAnswer; // file to write servers answer. should be often reopened

	QListIterator<ImageToUpload> *imageIterator; // iterates QList with images through uploads
	QList<UploadResult> uploadResult; // list of structures with data from xml
	QHash<QString, QString> *myCurrentResultHash; // hash about current file. Filled from xml parser

// functions
	QHttpRequestHeader headers(QString boundary, qint64 length); // collects headers for http
	PostData collectData(ImageToUpload imageStruct);//, QString boundary); // collects bytes for post data
	bool parse_xml(); // just parse server's answer. I think it should be renamed
	void uploadFinish();
	void fillStruct(UploadResult *Struct, QHash<QString, QString> *Hash);

signals:
	void totalError(QString errorMessage); // apocalypse :-)
	void fileError(int fileIndex, QString errorMessage); // can't upload file
	void uploadComplete(QList<UploadResult> result); // uploaded all files
	void fileUploaded(); // another file uploaded
	void fileUploaded(int index, UploadResult result);
	void startNextFileSig(int index); // sends index to application

protected:
	bool openDevice(QIODevice *buffer, QIODevice::OpenMode mode); // just try to reopen
				// file with emitting total Error im emergency
	QString randomString(int length); // nice function from kApplication :-)

};

class SaxHandler : public QXmlDefaultHandler 
{ 
public: 
	SaxHandler(QHash<QString, QString> *myCurrentResult); 

	bool startElement(const QString &namespaceURI, 
		const QString &localName, 
		const QString &qName, 
		const QXmlAttributes &attribs); 
	bool endElement(const QString &namespaceURI, 
		const QString &localName,
		const QString &qName); 
	bool characters(const QString &str); 
	bool fatalError(const QXmlParseException &exception);
	QString myErrorString;

private: 
//	QListView *listView; 
//	QListViewItem *currentItem;
	QHash<QString, QString> *myCurrentResult;
	QString currentElement;
};




#endif
